/*本工具库需要在其他js插件库之后引入！*/
/**
 * @desc 一行多个box的时候 设置相同高度，例如 $("#id").setSameBoxHeight()
 * @author libeina
 */
(function($) {
    $.fn.setSameBoxHeight = function() {
        var boxParent = $(this);
        //你自己的插件代码
        var boxHeightArr = [];
        var minWidth = $(window).width();
        //宽度小于768 高度auto
        if (minWidth >= 768) {
            boxParent.find(".same-height-box .ibox,.same-height-box .ibox .ibox-content").css("height", "auto");
            boxParent.find(".same-height-box").each(function() {
                var boxDom = $(this);
                boxHeightArr.push(boxDom.find(".ibox").height() - boxDom.find(".ibox-title").outerHeight());
            });
            boxParent.find(".same-height-box .ibox .ibox-content").css("height", Math.max.apply(null, boxHeightArr) + "px");
        }
        $(window).resize(function() {
            var boxHeightArr = [];
            minWidth = $(window).width();
            boxParent.find(".same-height-box .ibox,.same-height-box .ibox .ibox-content").css("height", "auto");
            if (minWidth >= 768) {
                boxParent.find(".same-height-box").each(function() {
                    var boxDom = $(this);
                    boxHeightArr.push(boxDom.find(".ibox").height() - boxDom.find(".ibox-title").outerHeight());
                });
                boxParent.find(".same-height-box .ibox .ibox-content").css("height", Math.max.apply(null, boxHeightArr) + "px");
            }
        });
    };
    $.fn.setMinFullHeight = function() {
        var boxParent = $(this);
        var minHeight = $(window).height();
        var minWidth = $(window).width();
        var iboxContentHeight = boxParent.find(".ibox-content").outerHeight();
        var height = minHeight - 40 > iboxContentHeight + 48 ? minHeight - 40 : iboxContentHeight + 48;
        boxParent.find(".ibox").css({
            "margin-bottom": 0,
            "background": "#fff",
            "height": height + "px"
        });
        if (minWidth < 768) {
            boxParent.find(".ibox").css({
                "margin-bottom": "25px",
                "height": "auto"
            });
        }
        $(window).resize(function() {
            minHeight = $(window).height();
            minWidth = $(window).width();
            boxParent.find(".ibox").css({
                "margin-bottom": 0,
                "height": "auto"
            });
            height = minHeight - 40 > boxParent.find(".ibox-content").outerHeight() + 48 ? minHeight - 40 : boxParent.find(".ibox-content").outerHeight() + 48;
            if (minWidth < 768) {
                boxParent.find(".ibox").css({
                    "margin-bottom": "25px",
                    "height": "auto"
                });
            } else {
                boxParent.find(".ibox").css({
                    "margin-bottom": 0,
                    "height": height + "px"
                });
            }
        });

    };
    $.fn.setMinPersentHeight = function(per, isLast) {
        var boxParent = $(this);
        var minHeight = $(window).height();
        var minWidth = $(window).width();
        var iboxContentHeight = boxParent.find(".ibox-content").outerHeight();
        var height = minHeight - 40 > iboxContentHeight + 48 ? minHeight - 40 : iboxContentHeight + 48;
        var marginBottom = 0;
        var trueHeight;
        if (!isLast) {
            marginBottom = 25;
            trueHeight = height * per / 100;
        } else {
            var tempHeight = 0;
            var broBox = boxParent.siblings();
            var len = broBox.length;
            for (var i = 0; i < len; i++) {
                tempHeight += parseInt($(broBox[i]).height());
            }
            trueHeight = height - tempHeight - len * 25;

        }
        boxParent.find(".ibox").css({
            "margin-bottom": marginBottom + "px",
            "background": "#fff",
            "min-height": trueHeight + "px"
        });
        if (minWidth < 768) {
            boxParent.find(".ibox").css({
                "margin-bottom": marginBottom + "px",
                "min-height": "auto"
            });
        }
        $(window).resize(function() {
            minHeight = $(window).height();
            minWidth = $(window).width();
            boxParent.find(".ibox").css({
                "margin-bottom": 0,
                "min-height": "auto"
            });
            height = minHeight - 40 > boxParent.find(".ibox-content").outerHeight() + 48 ? minHeight - 40 : boxParent.find(".ibox-content").outerHeight() + 48;
            if (!isLast) {
                marginBottom = 25;
                trueHeight = height * per / 100;
            } else {
                var tempHeight = 0;
                var broBox = boxParent.siblings();
                var len = broBox.length;
                for (var i = 0; i < len; i++) {
                    tempHeight += parseInt($(broBox[i]).height());
                }
                trueHeight = height - tempHeight - len * 25;

            }
            if (minWidth < 768) {
                boxParent.find(".ibox").css({
                    "margin-bottom": marginBottom + "px",
                    "min-height": "auto"
                });
            } else {
                boxParent.find(".ibox").css({
                    "margin-bottom": marginBottom + "px",
                    "min-height": trueHeight + "px"
                });
            }
        });
    };
    $.fn.placeholder = function(options) {
        var opts = $.extend({}, $.fn.placeholder.defaults, options);
        var isIE = document.all ? true : false;
        return this.each(function() {
            var _this = this,
                placeholderValue = _this.getAttribute("placeholder"); //缓存默认的placeholder值
            if (isIE) {
                _this.setAttribute("value", placeholderValue);
                _this.onfocus = function() {
                    $.trim(_this.value) == placeholderValue ? _this.value = "" : '';
                };
                _this.onblur = function() {
                    $.trim(_this.value) == "" ? _this.value = placeholderValue : '';
                };
            }
        });
    };

    //clear most dom value
    $.fn.clearMostDomValue = function() {
        var form = $(this);
        form.find("input[type!='button'],input[type!='submit'],input[type!='reset'],input[type!='hidden']").val('').removeAttr('checked').removeAttr('selected');
        form.find("textarea").val('');
    }
    $.getBrowserType = function() {
        var result = {};
        // Opera 8.0+
        var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
        result.isOpera = isOpera;

        // Firefox 1.0+
        var isFirefox = typeof InstallTrigger !== 'undefined';
        result.isFirefox = isFirefox;

        // Safari 3.0+ "[object HTMLElementConstructor]"
        var isSafari = /constructor/i.test(window.HTMLElement) || (function(p) {
            return p.toString() === "[object SafariRemoteNotification]";
        })(!window['safari'] || safari.pushNotification);
        result.isSafari = isSafari;

        // Internet Explorer 6-11
        var isIE = /*@cc_on!@*/ false || !!document.documentMode;
        result.isIE = isIE;
        if (isIE) {
            result.IENum = document.documentMode;
        }
        // Edge 20+
        var isEdge = !isIE && !!window.StyleMedia;
        result.isEdge = isEdge;

        // Chrome 1+
        var isChrome = !!window.chrome && !!window.chrome.webstore;
        result.isChrome = isChrome;
        // Blink engine detection
        //var isBlink = (isChrome || isOpera) && !!window.CSS;

        return result;
    }
    //数据筛选器
    $.extend({
        doFilter:function(options,data){
            var result = true;
            if(options.type && options.type == 'match'){
                var ifMatch = true;
                for(var i=0;i<options.param.length;i++){
                    if(!options.param[i].value){
                        continue;
                    }
                    var keyArr = options.param[i].key.split(",");
                    if(keyArr.length == 1){
                        if(typeof data[options.param[i].key] == 'object' ||
                            data[options.param[i].key].toUpperCase().indexOf(options.param[i].value.toUpperCase()) == -1){
                            ifMatch = false;
                        }
                    }else{
                        if((typeof data[keyArr[0]] == 'object' ||
                            data[keyArr[0]].toUpperCase().indexOf(options.param[i].value.toUpperCase()) == -1)&&
                            (typeof data[keyArr[1]] == 'object' ||
                                data[keyArr[1]].toUpperCase().indexOf(options.param[i].value.toUpperCase()) == -1)){
                            ifMatch = false;
                        }
                    }
                }
                if(!ifMatch){
                    result = false;
                }
            }
            if(options.type && options.type == 'range'){
                var ifRange = true;
                for(var i=0;i<options.param.length;i++){
                    var low = options.param[i].low ? options.param[i].low : 0;
                    var high = options.param[i].high ? options.param[i].high : 99999;
                    if(isNaN(parseFloat(data[options.param[i].key])) || data[options.param[i].key] < low || data[options.param[i].key] > high){
                        ifRange = false;
                    }
                }
                if(!ifRange){
                    result = false;
                }
            }
            return result;
        }
    });
})(jQuery);
$(function() {
    /*var browser = navigator.appName;

    var b_version = navigator.appVersion;

    var version = b_version.split(";");

    var trim_Version = version[1].replace(/[ ]/g, "");
    if (browser == "Microsoft Internet Explorer" && trim_Version == "MSIE9.0") {
        $("input").placeholder();
    }*/
    var browserType = $.getBrowserType();
    if (browserType && browserType.isIE && browserType.IENum == 9) {
        $("input").placeholder();
    }
});

function getUUID(len, radix) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    var uuid = [],
        i;
    radix = radix || chars.length;

    if (len) {
        // Compact form
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
    } else {
        // rfc4122, version 4 form
        var r;

        // rfc4122 requires these characters
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';

        // Fill in random data.  At i==19 set the high bits of clock sequence as
        // per rfc4122, sec. 4.1.5
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random() * 16;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }

    return uuid.join('');
}
//日期插件 前一天 后一天
function changeDate(dir, dom) {
    var date = $(dom).val();
    var dateAdd = addDate(date, dir);
    $(dom).val(dateAdd).trigger("changeDate");
}


// 日期，在原有日期基础上，增加days天数，默认增加1天
function addDate(date, days) {
    if (days == undefined || (days == '' && days != 0 )) {
        days = 1;
    }
    var date = new Date(date);
    date.setDate(date.getDate() + days);
    var month = date.getMonth() + 1;
    var day = date.getDate();
    return date.getFullYear() + '-' + getFormatDate(month) + '-' + getFormatDate(day);
}

// 日期，在原有日期基础上，增加days天数，默认增加1天  yy-dd
function addDateYYDD(date, days) {
    if (days == undefined || (days == '' && days != 0 )) {
        days = 1;
    }
    var date = new Date(date);
    date.setDate(date.getDate() + days);
    var month = date.getMonth() + 1;
    var day = date.getDate();
    return getFormatDate(month) + '-' + getFormatDate(day);
}

// 日期月份/天的显示，如果是1位数，则在前面加上'0'
function getFormatDate(arg) {
    if (arg == undefined || arg == '') {
        return '';
    }

    var re = arg + '';
    if (re.length < 2) {
        re = '0' + re;
    }

    return re;
}